<template>
    <div>
        <el-card class="box-card" shadow="never">
            <!--导航栏-->
            <el-page-header @back="back" :content="content">
            </el-page-header>
            <el-form ref="form" class="edit-el-form"  :model="form" :rules="rules" label-width="auto">
                <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="form.nickname" required></el-input>
                </el-form-item>
                <el-form-item label="评论内容" prop="content">
                    <el-input  type="textarea" :rows="3" v-model="form.content"  maxlength="300" show-word-limit required></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model.number="form.sort" required>
                      <template slot="append">注：排序越大越靠前，默认100</template>
                    </el-input>
                </el-form-item>
                <!--查看不展示按钮-->
                <el-form-item v-if="edit_type!=0">
                    <el-button type="primary" @click="submitForm('form')" :loading="loading">{{button_name}}</el-button>
                    <el-button @click="back">取消</el-button>
                </el-form-item>
            </el-form>
        </el-card>

    </div>
</template>

<script>
    export default {
        name:'CommentEdit',
        data() {
            return {
                form: {
                    nickname: '',
                    content:'',
                    sort: 100,
                },
                //校验规则
                rules:{
                    nickname: [
                        { required:true , message:'请输入昵称',trigger:'blur' }
                    ],
                    content:[
                        { required:true , message:'请输入内容',trigger:'blur' }
                    ],
                    sort:[
                        { required:true , message:'请输入排序号',trigger:'blur' },
                        { type:'number' , message:'排序号必须为数字' }
                    ]
                },
                //加载组件
                loading: false,
                button_name:'立即提交',
                content:'评论添加',
            }
        },
        methods: {
            submitForm(formName){
                let that = this;
                    this.$refs[formName].validate((valid) => {
                            if (valid) {
                                this.loading=true;
                                this.button_name='正在提交';
                                var send_data={
                                    api_token:this.$store.getters.getSessionId,
                                    nickname:this.form.nickname,
                                    content:this.form.content,
                                    sort:this.form.sort,
                                };
                                if(this.id!=-1){
                                    send_data.id=this.id;
                                }
                                this.$store.dispatch('editComment',send_data);
                            }else{
                                setTimeout(()=>{
                                  const is_error=document.getElementsByClassName('is-error');
                                  is_error[0].querySelector('input').focus();
                                },100);
                                return false;
                            }
                    })
            },
            back(){
                this.$router.go(-1);
            }
        },
        created(){
            if(this.id!=-1){
                var send_data={
                    api_token:this.$store.getters.getSessionId,
                    id:this.id
                }
                this.$store.dispatch('loadCommentInfo',send_data);
            }
            //修改标题
            if(this.edit_type==1){
                this.content='评论添加';
            }else if(this.edit_type==0){
                this.content='评论详情';
            }else if(this.edit_type==2){
                this.content='评论编辑';
            }
        },
        computed:{
            editCommentStatus(){
                return this.$store.getters.getEditCommentStatus;
            },
            commentinfo(){
                return this.$store.getters.getCommentInfo;
            },
            id(){
                return this.$route.params.id;
            },
            //是否可编辑
            edit_type(){
                return this.$route.params.edit_type;
            }
        },
        watch:{
            editCommentStatus(val){
                let that=this;
                if(val==1){
                    //按钮置为失效
                    this.$message({
                        message:this.content+'成功',
                        type:'success',
                        duration:1000,
                        onClose:function(e){
                            that.loading=false;
                            that.$store.commit('setEditCommentStatus',-1);
                            that.$router.push('/commentlist');
                        }
                    });
                }else{
                    this.$message({
                        message:this.content+'失败',
                        type:'error',
                        duration:2000
                    });
                    that.loading=false;
                    that.button_name='立即提交';
                    //初始化状态
                    that.$store.commit('setEditCommentStatus',-1);
                }
            },
            //加载到数据之后
            commentinfo(val){
              this.form.nickname=val.nickname;
              this.form.content=val.content;
              this.form.sort=val.sort;
            }
        }
    }
</script>

<style>
    .edit-el-form{
        margin-top:30px;
    }
</style>
